from typing import Dict
import asyncio

from sanic import Blueprint, HTTPMethod, json, HTTPResponse
from sanic.request import Request
from sanic.exceptions import SanicException


user_blueprint = Blueprint("users", url_prefix="/users")

# @user_blueprint.post("/user/register")
# async def register(request: Request) -> Dict:
#     """
#     注册
#     """
#     return await services.register(request)
#
#
# @user_blueprint.post("/user/login")
# async def login(request: Request) -> Dict:
#     """
#     登录
#     """
#     return await services.login(request)
#
#
# @user_blueprint.get("/user/profile")
# async def profile(request: Request) -> Dict:
#     """
#     获取本人信息
#     """
#     return await services.profile(request)

@user_blueprint.get("/get_info")
async def register(request: Request) -> HTTPResponse:
    """
    注册
    """
    response = json({
        "user": "hahaha",
    })
    print(request.cookies)
    print(response.cookies)
    # response.cookies["aaa"] = "aaa"
    await request.app.dispatch(
        "user.registration.created",
        context={"email": "abc@123.com"}
    )
    await request.app.ctx.redis.set("abc", "abc")
    return response


@user_blueprint.websocket("/ws_recv_send")
async def ws_recv_send(request: Request, ws):
    while True:
        data = "hello!"
        print("Sending: " + data)
        await ws.send(data)
        await asyncio.sleep(1)
        data = await ws.recv()
        print("Received: " + data)
